﻿//-----------------------------------------------------------------------
// <copyright file="ViewModelBase.cs" company="Microsoft">
//     Copyright (c) AGALTIER, Microsoft. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace ArchiSimple.Windows
{
    using System;
using System.Collections.Generic;
using System.Linq;
using ArchiSimple.Windows.Message;
//using Ninject.Extensions.Interception;
//using Ninject.Extensions.Interception.Attributes;

    /// <summary>
    /// </summary>
    public abstract class ViewModelBase : GalaSoft.MvvmLight.ViewModelBase //, IAutoNotifyPropertyChanged
    {
        public virtual void Init()
        {
            if (this.NavigationAction != null)
            {
                MessengerInstance.Register(this, NavigationAction);
            }
        }

        protected void NavigateTo(string name)
        {
            MessengerInstance.Send<NavigationMessage>(new NavigationMessage { Name = name });
        }

        protected Action<NavigationMessage> NavigationAction { get; set; }

        protected void Send<T>(T message)
        {
            MessengerInstance.Send<T>(message);
        }

        protected void Register<T>(Action<T> action)
        {
            MessengerInstance.Register<T>(this, action);
        }

        public void OnPropertyChanged(string propertyName)
        {
            this.RaisePropertyChanged(propertyName);
        }

        private Dictionary<string, object> dicoProperty = new Dictionary<string, object>();

        protected T Get<T>(string propertyName)
        {
            if (this.dicoProperty.ContainsKey(propertyName))
            {
                return (T)this.dicoProperty[propertyName];
            }
            else
            {
                return default(T);
            }
        }

        protected void Set<T>(string propertyName, T value)
        {
            T oldValue = Get<T>(propertyName);
            this.dicoProperty[propertyName] = value;
            this.Set<T>(propertyName, ref oldValue, value);
        }
    }
}
